package org.gis.mapserver.rendercontext;

import java.util.Map;
import org.gis.mapserver.base.ContextType;
import org.gis.mvc.bean.RequestBean;
import org.gis.utils.activator.Activator;

/**
 *
 * @author gus
 */
public class RenderingContextFactory
{
    private Map<String, String> contextMapping;

    public void setContextMapping( Map<String, String> contextMapping )
    {
        this.contextMapping = contextMapping;
    }

    public RenderingContext createRenderingContext( RequestBean requestBean ) throws Exception
    {
        if ( requestBean == null )
        {
            throw new IllegalArgumentException("requestBean");
        }

        RenderingContextBuilder builder = getContextBuilder(requestBean);
        return builder.makeRenderingContext();
    }

    private RenderingContextBuilder getContextBuilder( RequestBean requestBean ) throws Exception
    {
        ContextType contextType =  requestBean.getContextType();
        if ( contextType == null )
        {
            throw new Exception("cant not find ContextType");
        }

        String builderClass = contextMapping.get(contextType.name());
        if ( builderClass == null || builderClass.isEmpty() )
        {
            throw new Exception(String.format("context %s  not supported", contextType));
        }
        return (RenderingContextBuilder) Activator.instance(builderClass, new Object[]{requestBean});
    }
}
